//
//  SettingViewController.m
//  MZSystem
//
//  Created by 智障 on 2018/11/5.
//  Copyright © 2018 zerod. All rights reserved.
//

#import "SettingViewController.h"
#import "SettingDetail_ViewController.h"

#import "LoginViewController.h"

#import "Setting_TableViewCell.h"

#import "MZGlobleConfig.h"

@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>
@property (nonatomic ,strong) UITableView    *tableView;
@property (nonatomic ,strong) NSMutableArray *dataArray;

@property (nonatomic ,strong) Base_CenterNavView *navView;
@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.navView];
    [self.view addSubview:self.tableView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}
-(UITableView*)tableView{
    if(!_tableView){
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREEN_WIDTH, ScrH - SafeAreaTopHeight - SafeAreaBottomHeight) style:UITableViewStylePlain];
        _tableView.showsVerticalScrollIndicator=NO;
        _tableView.showsHorizontalScrollIndicator=NO;
        _tableView.backgroundColor = RGB(239, 239, 239);
        _tableView.separatorStyle = 0;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:@"Setting_TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        
    }
    return _tableView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    Setting_TableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[Setting_TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    if (indexPath.section == 0) {
        cell.titleLab.text = @"音频视频质量设置";
        cell.contentL.text = [NSString stringWithFormat:@"%.0f*%.0f",[MZGlobleConfig defaultManager].videoSize.width,[MZGlobleConfig defaultManager].videoSize.height];
        cell.titleLab.hidden = NO;
        cell.contentL.hidden = NO;
        cell.imageNex.hidden = NO;
        cell.loginOutLabel.hidden = YES;
    }
    if (indexPath.section == 1) {
        cell.titleLab.text = @"联系我们";
        cell.contentL.hidden = YES;
        cell.imageNex.hidden = YES;
        cell.loginOutLabel.hidden = YES;
    }
    if (indexPath.section == 3) {
        cell.titleLab.hidden =YES;
        cell.contentL.hidden = YES;
        cell.imageNex.hidden = YES;
        cell.loginOutLabel.hidden = NO;
    }
    return cell;
}
-  (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50.f;
}
- (CGFloat )tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0 || section == 1) {
        return 10.f;
    }else{
        return 0.f;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 0 || section == 1) {
        return [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScrW, 10.f)];
    }else{
        return nil;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        SettingDetail_ViewController *vc = [SettingDetail_ViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (indexPath.section == 1) {
        NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",@"057185310049"];
        UIWebView * callWebview = [[UIWebView alloc] init];
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
        [self.view addSubview:callWebview];
    }
    if (indexPath.section == 2) {
        UIAlertView*alertView=[[UIAlertView alloc]initWithTitle:@"是否退出登录" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
        [alertView show];
    }
}
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        [self requestLoginOut];
    }
}
- (Base_CenterNavView *)navView{
    if (!_navView) {
        _navView = [Base_loadXibView loadFromXib:@"Base_CenterNavView" withCls:[Base_CenterNavView class]];
        _navView.navTitle.text = @"设置";
        [_navView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SafeAreaTopHeight)];
        _navView.rightbutton.hidden = YES;
        WS(weakSelf)
        _navView.clickBack = ^{
            [weakSelf.navigationController popViewControllerAnimated:YES];
        };
    }
    return _navView;
}
- (void)requestLoginOut{
    NSString*url=[NSString stringWithFormat:@"%@/logout",URL_HOST];
    NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSArray *cookieArray = [NSArray arrayWithArray:[cookieJar cookies]];
    for(id obj in cookieArray)
    {
        [cookieJar deleteCookie:obj];
    }

    [SendRequest postWithURL:url params:nil withCookie:YES success:^(id response) {
        int code = [response[@"code"] intValue];
        NSString *message = response[@"msg"];
        if(code==0){
            //移除cookie
            [CoreArchive removeStrForKey:COOKIE];

           AppRootViewController = [LoginViewController new];
            
        }else{
            SHOWERROR(message)
        }
    } fail:^(NSError *error) {
        SHOWERROR(showNetworkError)
    }];
}
@end
